package com.sharker;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.widget.Button;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.sharker.activity.BaseActivity;
import com.sharker.fragment.CourseFragment;
import com.sharker.fragment.LearnFragment;
import com.sharker.fragment.PeopleFragment;
import com.sharker.models.data.BaseData;
import com.sharker.models.FirstHand;
import com.sharker.models.data.TryData;
import com.sharker.network.SharkerParams;
import com.zhy.autolayout.AutoRelativeLayout;

import org.xutils.common.Callback;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener{

    @BindView(R.id.fl)
    FrameLayout fl;
    /* @BindView(R.id.btn_learn)
     Button btnLearn;
     @BindView(R.id.btn_course)
     Button btnCourse;
     @BindView(R.id.btn_people)*/
    Button btnPeople;
    @BindView(R.id.activity_main)
    AutoRelativeLayout activityMain;
    @BindView(R.id.mBottombar)
    BottomNavigationBar mBottombar;
    private LearnFragment learnFragment;
    private CourseFragment courseFragment;
    private PeopleFragment peopleFragment;
    public static final int FRAGMENT_LEARN = 0;
    public static final int FRAGMENT_COURSE = 1;
    public static final int FRAGMENT_PEOPLE = 2;
    public int position;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      /*  if (Build.VERSION.SDK_INT >= 21) {
            *//*View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);*//*
           getWindow().setNavigationBarColor(Color.TRANSPARENT);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }*/
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        ButterKnife.bind(this);
        FirstHand.getInstance();
        if (FirstHand.isHost()) {
          listBanner();listTry();

        } else {
            firstHand();
        }
        showFragment(FRAGMENT_LEARN);
        initview();

    }

    private void initview(){
        mBottombar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        mBottombar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottombar
                .addItem(new BottomNavigationItem(R.mipmap.study2xb,"学习")).setActiveColor(R.color.colorYellow)
                .addItem(new BottomNavigationItem(R.mipmap.course2xb,"课程")).setActiveColor(R.color.colorYellow)
                .addItem(new BottomNavigationItem(R.mipmap.person2xb,"我的")).setActiveColor(R.color.colorYellow)
                .initialise();
        mBottombar.setTabSelectedListener(this);

    }
    public void firstHand() {
        SharkerParams params = new SharkerParams("first_hand");
        x.http().post(params, new Callback.CommonCallback<FirstHand>() {
            @Override
            public void onSuccess(FirstHand result) {
                FirstHand.save(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                getHost();
            }
        });

    }

    void getHost() {
        SharkerParams params = new SharkerParams("get_host");
        x.http().post(params, new Callback.CommonCallback<FirstHand>() {
            @Override
            public void onSuccess(FirstHand result) {
                FirstHand.saveHost(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                listBanner();
                listTry();
            }
        });
    }

    void listBanner() {
        SharkerParams params = new SharkerParams("list_banner");
        x.http().post(params, new Callback.CommonCallback<BaseData>() {
            @Override
            public void onSuccess(BaseData result) {
                Log.i("djn", result.toString());

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    void listTry() {
        SharkerParams params = new SharkerParams("list_try");
//        params.addBodyParameter("category","001");
        params.addBodyParameter("page_size", "20");
        params.addBodyParameter("page_index", "0");
        x.http().post(params, new Callback.CommonCallback<TryData>() {
            @Override
            public void onSuccess(TryData result) {
                Log.i("TAG", "onSuccess: " + result.toString());

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void showFragment(int Index) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        hideFragment(ft);
        position = Index;
        switch (position) {
            case FRAGMENT_LEARN:
                if (learnFragment == null) {
                    learnFragment = new LearnFragment();
                    ft.add(R.id.fl, learnFragment);
                } else {
                    ft.show(learnFragment);
                }
                break;
            case FRAGMENT_COURSE:
                if (courseFragment == null) {
                    courseFragment = new CourseFragment();
                    ft.add(R.id.fl, courseFragment);
                } else {
                    ft.show(courseFragment);
                }
                break;
            case FRAGMENT_PEOPLE:
                if (peopleFragment == null) {
                    peopleFragment = new PeopleFragment();
                    ft.add(R.id.fl, peopleFragment);
                } else {
                    ft.show(peopleFragment);
                }
                break;
        }
        ft.commit();


    }

    private void hideFragment(FragmentTransaction ft) {
        if (learnFragment != null) {
            ft.hide(learnFragment);
        }
        if (courseFragment != null) {
            ft.hide(courseFragment);
        }
        if (peopleFragment != null) {
            ft.hide(peopleFragment);
        }
    }

    @Override
    public void onTabSelected(int position) {
        switch(position){
            case FRAGMENT_LEARN:
                showFragment(FRAGMENT_LEARN);
                break;
            case FRAGMENT_COURSE:
                showFragment(FRAGMENT_COURSE);
                break;
            case FRAGMENT_PEOPLE:
                showFragment(FRAGMENT_PEOPLE);
                break;
        }

    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
   /* @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }*/
}
